﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace Atentis_Connector
{
	public partial class StrategyParamsInputForm : Form
	{
		/// <summary>
		/// Событие - обновлены настройки стратегии
		/// </summary>
		public event EventHandler StrategySettingsUpdated;

		void OnStrategySettingsUpdated(AtentisConnectorEventArgs e)
		{
			EventHandler strategySettingsUpdated = StrategySettingsUpdated;
			if (strategySettingsUpdated != null)
				strategySettingsUpdated(this, e);
		}

		public StrategyParamsInputForm()
		{
			InitializeComponent();
		}

		public StrategyParamsInputForm(StrategySettings settings)
		{
			InitializeComponent();
			periodTextBox.Text = settings.Period.ToString();
			tresholdTextBox.Text = settings.Threshold.ToString();
			slTextBox.Text = settings.StopLoss.ToString();
			tpTextBox.Text = settings.TakeProfit.ToString();
			minCandleTextBox.Text = settings.MinCandleSize.ToString();
			volTextBox.Text = settings.Vol.ToString();
			useAOСheckBox.Checked = settings.UseAO;
			useFraqPosCheckBox.Checked = settings.UseFraqPosition;
		}

		private void acceptParamsButton_Click(object sender, EventArgs e)
		{
			try
			{
				var s = minCandleTextBox.Text.Replace('.', ',');
				var minCandle = int.Parse(s);
				s = volTextBox.Text.Replace('.', ',');
				var vol = int.Parse(s);
				s = tpTextBox.Text.Replace('.', ',');
				var take = decimal.Parse(s);
				var period = int.Parse(periodTextBox.Text);
				s = tresholdTextBox.Text.Replace('.', ',');
				var threshold = decimal.Parse(s);
				s = slTextBox.Text.Replace('.', ',');
				var stoploss = decimal.Parse(s);
				var useAO = useAOСheckBox.Checked;
				var useFPos = useFraqPosCheckBox.Checked;
				var strstg = new StrategySettings(vol, minCandle, take, period, threshold, stoploss, useAO, useFPos);

				var acea = new AtentisConnectorEventArgs(strstg);
				OnStrategySettingsUpdated(acea);
				Close();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Ошибка в методе 'acceptParamsButton_Click': "+ex.Message);
			}
		}

		private void periodTextBox_KeyDown(object sender, KeyEventArgs e)
		{
			CheckButton(e);
		}

		private void tresholdTextBox_KeyDown(object sender, KeyEventArgs e)
		{
			CheckButton(e);
		}

		private void slTextBox_KeyDown(object sender, KeyEventArgs e)
		{
			CheckButton(e);
		}

		private void CheckButton(KeyEventArgs e)
		{
			if (e.KeyData == Keys.Enter)
				acceptParamsButton.PerformClick();
		}

	}
}
